﻿using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Caching;
using Volo.Abp.Modularity;

namespace My.RedisUtils
{
    [DependsOn(typeof(AbpCachingModule))]
    public class MyCachingStackExchangeRedisModule : AbpModule
    {

        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            Configure<AbpDistributedCacheOptions>(options =>
            {
                options.KeyPrefix = "TingjianGas:";
                options.HideErrors = false;
            });



            IConfiguration configuration = context.Services.GetConfiguration();
            var redisEnabled = configuration["Redis:IsEnabled"];
            if (!redisEnabled.IsNullOrEmpty() && bool.TryParse(redisEnabled, out bool redisOpen))
            {
                context.Services.AddStackExchangeRedisCache(options =>
                {
                    var redisConfiguration = configuration["Redis:Configuration"];
                    if (!redisConfiguration.IsNullOrEmpty())
                    {
                        options.Configuration = redisConfiguration;
                    }
                });

                context.Services.Replace(ServiceDescriptor.Singleton<IDistributedCache, MyRedis>());


            }
        }

    }
}
